package cn.edu.sdjzu.xg.kcsj.service;


import cn.edu.sdjzu.xg.kcsj.dao.StudentDao;
import cn.edu.sdjzu.xg.kcsj.domain.Student;
import cn.edu.sdjzu.xg.kcsj.domain.User;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;


public final class StudentService {
    private static  StudentService studentService = null;
    private StudentService() {
    }

    public static StudentService getInstance() {
        if (studentService == null){
            studentService = new StudentService();
        }
        return studentService;
    }

    /**
     * 根据条件查找Student对象
     *
     * @return 查询结果
     * @throws SQLException
     */
    public Collection<Student> findAll() throws SQLException {
        //获得连接以便完成可能的事务操作
        Connection connection = JdbcHelper.getConn();
        Collection<Student> students = null;
        students = StudentDao.getInstance().findAll(null, null,connection);
        JdbcHelper.close(connection);
        return students;
    }

    /**
     * 根据id查找Student对象
     *
     * @param id 要查询的Student对象的id
     * @return 查询结果
     * @throws SQLException
     */
    public Student find(Integer id) throws SQLException {
        //获得connection资源
        Connection connection = JdbcHelper.getConn();
        Student student =  StudentDao.getInstance().find(id, connection);
        JdbcHelper.close(connection);
        return student;
    }

    /**
     * 根据对象修改Student对象
     *
     * @param student 要修改的Student对象
     * @return 修改成功返回true，否则false
     * @throws SQLException
     */
    public boolean update(Student student) throws SQLException {
        //获得connection资源
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        boolean result_update = false;
        try {
            result_update = StudentDao.getInstance().update(student,connection);
            //提交事务
            connection.commit();

        }catch (SQLException e){
            //回滚事务中所有操作
            e.printStackTrace();
            connection.rollback();
        }finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return result_update;
    }

    /**
     * 根据id删除Student对象
     *
     * @param id 要删除的Student对象
     * @return 删除成功返回true，否则false
     * @throws SQLException
     */
    public boolean delete(Integer id) throws SQLException{
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        boolean result_del = false;
        try {
            result_del = StudentDao.getInstance().delete(id,connection);
            //提交事务
            connection.commit();
        }catch (SQLException e){
            //回滚事务中所有操作
            e.printStackTrace();
            connection.rollback();
        }finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(null, connection);
        }
        return result_del;
    }

    public boolean deletes(List<Student> listToDelete) throws SQLException{
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean deleted =false;
        try {
            for(Student e:listToDelete) {
                int id= e.getId();
                //删除用户
                deleted = StudentDao.getInstance ().delete(id, connection);
            }
            //提交事务
            connection.commit();
        }catch (Exception e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return deleted ;
    }

    /**
     * 添加Student对象，该对象id的值取自它超类对象的id值
     *
     * @param student 要添加的Student对象
     * @return 添加成功返回true，否则false
     * @throws SQLException
     */
    public boolean add(Student student) throws SQLException {
        //获得进行事务的连接，添加学生和添加用户都使用本连接对象
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        boolean added = false;
        try {
            student.setId(ActorService.getInstance().add());
            //创建用户对象，密码和用户名取自student字段值no
            User user = new User(student.getNo(), student.getNo(), 3,null, student);
            Integer id = UserService.getInstance().add(user);
            //为student的id赋值
            student.setId(id);
            added=StudentDao.getInstance().add(student, connection);
            //提交事务
            connection.commit();
        } catch (SQLException e) {
            //回滚事务中所有操作
            e.printStackTrace();
            connection.rollback();
        } finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return added;
    }
}
